<script setup>
import api from '@/api/index.js'
import { message } from 'ant-design-vue'
import { computed, ref, h, provide } from 'vue'
import MyTable from '@/components/MyTable/MyTable.vue'
import { Modal } from 'ant-design-vue'
import { getConsants } from '@/utils'
import ImageView from '@/components/ImageView/ImageView.vue'

const props = defineProps({
  // 表格数据总条数
  total: { type: Number, default: 0 },
  // 表格当前页码
  pageNum: { type: Number, default: 1 }
})
// 分页参数
const pagination = computed(() => ({
  total: props.total,
  showSizeChanger: false,
  current: props.pageNum,
  onChange: (e) => {
    emits('pageChange', e)
  }
}))

const emits = defineEmits(['pd', 'xq', 'pageChange', 'reload'])

// 表格列配置
const columns = ref([
  { ellipsis: true, title: '工单编号', dataIndex: 'orderNo', key: 'orderNo' },
  { ellipsis: true, title: '农机类型', key: 'orderType' },
  { ellipsis: true, title: '土地类型', key: 'fieldType' },
  { ellipsis: true, title: '农机名称', dataIndex: 'machineName', key: 'machineName' },
  { ellipsis: true, title: '耕作面积(亩)', dataIndex: 'farmingArea', key: 'farmingArea' },
  { ellipsis: true, title: '加油数量(升)', key: 'fuelValue' },
  { ellipsis: true, title: '加油金额(元)', key: 'fuelMoney' },
  { ellipsis: true, title: '加油照片', key: 'fileInfo' },
  {
    ellipsis: true,
    title: '作业单价(元)',
    dataIndex: 'realityFarmingPrice',
    key: 'realityFarmingPrice'
  },
  {
    ellipsis: true,
    title: '实际作业面积(亩)',
    dataIndex: 'realityFarmingArea',
    key: 'realityFarmingArea'
  },
  {
    ellipsis: true,
    title: '实际作业金额(亩)',
    dataIndex: 'finalSettlementMoney',
    key: 'actualAmount'
  },
  { ellipsis: true, title: '机手提成', key: 'handheldMachine' },
  { ellipsis: true, title: '工单状态', key: 'orderStatus1' },
  { ellipsis: true, title: '备注', dataIndex: 'orderRemark', key: 'orderRemark' },
  { ellipsis: true, title: '操作', key: 'set', width: 220, align: 'center' }
])

// 派单
const pd = (item) => {
  emits('pd', item)
}

// 数据详情
const xq = (item) => {
  emits('xq', item)
}

//实际作业金额
// const actualAmount = (record) => {
//   return record.realityFarmingArea * record.realityFarmingPrice
// }

//删除数据
const del = (item) => {
  Modal.warning({
    title: '删除工单',
    content: h(
      'span',
      {},
      '是否删除工单：',
      h('span', { style: { color: '#09c690' } }, item.orderNo)
    ),
    okText: '删除',
    okCancel: true,
    okButtonProps: { type: 'primary', danger: true },
    cancelButtonProps: { type: 'primary' },
    cancelText: '取消',
    onOk: async () => {
      await api.delFar({ id: item.orderId })
      message.success('删除成功')
      emits('reload')
      return Promise.resolve()
    },
    onCancel: () => {
      message.success('取消删除')
      return Promise.resolve()
    }
  })
}

const getInfos = ref({})
const getInfoLoading = ref(false)
const openChange = async (bool, record) => {
  if (bool && !getInfos.value[record.orderNo]) {
    getInfoLoading.value = true
    try {
      const {
        resultObject: { records }
      } = await api.getLywzList({ orderNo: record.orderNo, size: 9999 })
      getInfos.value[record.orderNo] = records
      getInfoLoading.value = false
    } catch (error) {
      getInfoLoading.value = false
    }
  }
}
const extractUrl = (fileInfo) => {
  if (fileInfo) {
    const url = JSON.parse(fileInfo)[0].url
    return url
  } else {
    return
  }
}
provide('columns', columns.value)
</script>

<template>
  <MyTable :pagination="pagination" :scroll="{ y: 'calc(100vh - 383px)' }">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'handheldMachine'">
        {{ record.finalSettlementMoney * 0.1 }}
      </template>
      <template v-if="column.key === 'fuelValue'">
        {{ record.farmingWorkOrderFuel?.fuelValue }}
      </template>
      <template v-if="column.key === 'fuelMoney'">
        {{ record.farmingWorkOrderFuel?.fuelMoney }}
      </template>
      <template v-if="column.key === 'fileInfo'">
        <!-- {{ record.farmingWorkOrderFuel?.fileInfo }} -->
        <ImageView :value="extractUrl(record.farmingWorkOrderFuel?.fileInfo)" />
      </template>
      <template v-if="column.key === 'info'">
        <a-popover
          :overlay-inner-style="{ padding: 0 }"
          placement="left"
          @openChange="openChange($event, record)"
        >
          <template #content>
            <a-spin :spinning="getInfoLoading" wrapperClassName="com-spin">
              <a-empty
                v-if="!(getInfos?.[record.orderNo]?.length > 0)"
                style="padding-top: 30px"
              ></a-empty>
              <a-timeline style="padding: 20px">
                <a-timeline-item v-for="item of getInfos[record.orderNo]" :key="item.id">
                  <!-- <div>物资大类型：{{ item.outBigTypeName }}</div> -->
                  <!-- <div>物资小类型：{{ item.outSmallTypeName }}</div> -->
                  <div>物资名称：{{ item.productName }}</div>
                  <div>数量：{{ item.quantity }}{{ item.unit }}</div>
                  <div>单价：{{ item.price }}元</div>
                  <div>总价：{{ Number(item.quantity) * Number(item.quantity) }}元</div>
                  <div>
                    附件：
                    <a-image :src="item.annex" v-if="item.annex" :width="30" :height="30" />
                    <span v-else>--</span>
                  </div>
                </a-timeline-item>
              </a-timeline>
            </a-spin>
          </template>
          <span style="color: #09c690">领用物资</span>
        </a-popover>
      </template>
      <template v-if="column.key === 'orderType'">
        {{ getConsants('machine_type', record.orderType) }}
      </template>
      <template v-if="column.key === 'fieldType'">
        {{ getConsants('field_type', record.fieldType) }}
      </template>
      <template v-if="column.key === 'orderStatus1'">
        {{ getConsants('order_status1', record.orderStatus) }}
      </template>
      <template v-if="column.key === 'set'">
        <AButton type="link" @click="xq(record)">详情</AButton>
        <AButton
          type="link"
          @click="pd(record)"
          v-if="record?.orderStatus === 1 || record?.orderStatus === 2"
          >派单</AButton
        >
        <AButton type="link" class="com-delbtn" @click="del(record)">删除</AButton>
      </template>
    </template>
  </MyTable>
</template>
